{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "."
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Setting up the test\n",
      "Tearing down the test\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.001s\n",
      "\n",
      "OK\n"
     ]
    }
   ],
   "source": [
    "import unittest\n",
    "\n",
    "class TestAddition(unittest.TestCase):\n",
    "    def setUp(self):\n",
    "        print('Setting up the test')\n",
    "\n",
    "    def tearDown(self):\n",
    "        print('Tearing down the test')\n",
    "\n",
    "    def test_twoPlusTwo(self):\n",
    "        total = 2+2\n",
    "        self.assertEqual(4, total);\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=[''], exit=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "."
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Setting up the test\n",
      "Tearing down the test\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "..\n",
      "----------------------------------------------------------------------\n",
      "Ran 3 tests in 0.723s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
     ]
    }
   ],
   "source": [
    "from urllib.request import urlopen\n",
    "from bs4 import BeautifulSoup\n",
    "import unittest\n",
    "\n",
    "class TestWikipedia(unittest.TestCase):\n",
    "    bs = None\n",
    "    def setUpClass():\n",
    "        url = 'http://en.wikipedia.org/wiki/Monty_Python'\n",
    "        TestWikipedia.bs = BeautifulSoup(urlopen(url), 'html.parser')\n",
    "\n",
    "    def test_titleText(self):\n",
    "        pageTitle = TestWikipedia.bs.find('h1').get_text()\n",
    "        self.assertEqual('Monty Python', pageTitle);\n",
    "\n",
    "    def test_contentExists(self):\n",
    "        content = TestWikipedia.bs.find('div',{'id':'mw-content-text'})\n",
    "        self.assertIsNotNone(content)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=[''], exit=False)\n",
    "    %reset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlopen\n",
    "from bs4 import BeautifulSoup\n",
    "import unittest\n",
    "import re\n",
    "import random\n",
    "from urllib.parse import unquote\n",
    "\n",
    "class TestWikipedia(unittest.TestCase):\n",
    "\n",
    "    def test_PageProperties(self):\n",
    "        self.url = 'http://en.wikipedia.org/wiki/Monty_Python'\n",
    "        #Test the first 10 pages we encounter\n",
    "        for i in range(1, 10):\n",
    "            self.bs = BeautifulSoup(urlopen(self.url), 'html.parser')\n",
    "            titles = self.titleMatchesURL()\n",
    "            self.assertEqual(titles[0], titles[1])\n",
    "            self.assertTrue(self.contentExists())\n",
    "            self.url = self.getNextLink()\n",
    "        print('Done!')\n",
    "\n",
    "    def titleMatchesURL(self):\n",
    "        pageTitle = self.bs.find('h1').get_text()\n",
    "        urlTitle = self.url[(self.url.index('/wiki/')+6):]\n",
    "        urlTitle = urlTitle.replace('_', ' ')\n",
    "        urlTitle = unquote(urlTitle)\n",
    "        return [pageTitle.lower(), urlTitle.lower()]\n",
    "\n",
    "    def contentExists(self):\n",
    "        content = self.bs.find('div',{'id':'mw-content-text'})\n",
    "        if content is not None:\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "    def getNextLink(self):\n",
    "        # Returns random link on page, using technique from Chapter 3\n",
    "        links = self.bs.find('div', {'id':'bodyContent'}).find_all('a', href=re.compile('^(/wiki/)((?!:).)*$'))\n",
    "        randomLink = random.SystemRandom().choice(links)\n",
    "        return 'https://wikipedia.org{}'.format(randomLink.attrs['href'])\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=[''], exit=False)\n",
    "    %reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello there, Ryan Mitchell!\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "\n",
    "driver = webdriver.Chrome(\n",
    "    executable_path='drivers/chromedriver',\n",
    "    options=chrome_options)\n",
    "driver.get('http://pythonscraping.com/pages/files/form.html')\n",
    "\n",
    "firstnameField = driver.find_element_by_name('firstname')\n",
    "lastnameField = driver.find_element_by_name('lastname')\n",
    "submitButton = driver.find_element_by_id('submit')\n",
    "\n",
    "### METHOD 1 ###\n",
    "firstnameField.send_keys('Ryan')\n",
    "lastnameField.send_keys('Mitchell')\n",
    "submitButton.click()\n",
    "################\n",
    "\n",
    "### METHOD 2 ###\n",
    "#actions = ActionChains(driver).click(firstnameField).send_keys('Ryan').click(lastnameField).send_keys('Mitchell').send_keys(Keys.RETURN)\n",
    "#actions.perform()\n",
    "################\n",
    "\n",
    "print(driver.find_element_by_tag_name('body').text)\n",
    "\n",
    "driver.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      ".\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 1.938s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestAddition(unittest.TestCase):\n",
    "    driver = None\n",
    "\n",
    "    def setUp(self):\n",
    "        chrome_options = Options()\n",
    "        chrome_options.add_argument(\"--headless\")\n",
    "        self.driver = webdriver.Chrome(\n",
    "            executable_path='drivers/chromedriver',\n",
    "            options=chrome_options)\n",
    "        url = 'http://pythonscraping.com/pages/javascript/draggableDemo.html'\n",
    "        self.driver.get(url)\n",
    "\n",
    "    def tearDown(self):\n",
    "        self.driver.close()\n",
    "\n",
    "    def test_drag(self):\n",
    "        element = self.driver.find_element_by_id(\"draggable\")\n",
    "        target = self.driver.find_element_by_id(\"div2\")\n",
    "        actions = ActionChains(self.driver)\n",
    "        actions.drag_and_drop(element, target).perform()\n",
    "        self.assertEqual(\"You are definitely not a bot!\",\n",
    "                         self.driver.find_element_by_id(\"message\").text)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=[''], exit=False)\n",
    "    %reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      ".\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 1.940s\n",
      "\n",
      "OK\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import unittest\n",
    "\n",
    "class TestDragAndDrop(unittest.TestCase):\n",
    "    driver = None\n",
    "    def setUp(self):\n",
    "        chrome_options = Options()\n",
    "        chrome_options.add_argument(\"--headless\")\n",
    "        self.driver = webdriver.Chrome(\n",
    "            executable_path='drivers/chromedriver',\n",
    "            options=chrome_options)\n",
    "        url = 'http://pythonscraping.com/pages/javascript/draggableDemo.html'\n",
    "        self.driver.get(url)\n",
    "\n",
    "    def tearDown(self):\n",
    "        self.driver.close()\n",
    "\n",
    "    def test_drag(self):\n",
    "        element = self.driver.find_element_by_id('draggable')\n",
    "        target = self.driver.find_element_by_id('div2')\n",
    "        actions = ActionChains(self.driver)\n",
    "        actions.drag_and_drop(element, target).perform()\n",
    "        self.assertEqual('You are definitely not a bot!',\n",
    "            self.driver.find_element_by_id('message').text)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    unittest.main(argv=[''], exit=False)\n",
    "    %reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
